package org.restql.db.resource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.restql.db.Utility;
import org.restql.db.WhereClauseGenerator;

public class ReadOnlyDatabaseResourceImpl extends ReadOnlyDatabaseResource {

	List<String> select;
	List<String> from;
	List<String> where;
	String resourceName;
	String dbName;
	DataSource dataSource;

	public void setSelect(List<String> select) {
		this.select = select;
	}
	public void setFrom(List<String> from) {
		this.from = from;
	}
	public void setWhere(List<String> where) {
		this.where = where;
	}
	public String getResourceName() {
		return resourceName;
	}
	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}
	public String getDbName() {
		return dbName;
	}
	public void setDbName(String dbName) {
		this.dbName = dbName;
	}
	public DataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public String getFromString(RequestResource requestResource) {
		return "FROM " + Utility.commas(this.from);
	}

	public String getSelectString(RequestResource requestResource) {
		return "SELECT " + Utility.commas(this.select);
	}

	public String getWhereString(RequestResource requestResource) {
		String predicate = " ";
		if (this.where.size() > 0) {
			predicate += "WHERE " + Utility.ands(this.where);
		}
		return predicate + WhereClauseGenerator.generateWhereClause(
				requestResource.getQueryParameters(), (this.where.size() > 0));
	}

	public Connection getConnection(RequestResource requestResource) throws SQLException {
		return dataSource.getConnection();
	}
	
}
